# -*- coding: utf-8 -*-
#from __future__ import unicode_literals

from django.shortcuts import render

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from live_api import pili
from live_api.tools.Output_Helper import *
from live_api.tools.constant import *
import live_api.tools.utils_file as ufile
import os
import random
import time
import sys

reload(sys)
sys.setdefaultencoding("utf-8")


def env(key):
    if key in os.environ:
        return os.environ[key]
    else:
        return ""




@csrf_exempt
def glist_info(request,id):
    ret = ReturnJ()
    db =DB.getDB()
    try:
        sql = "select * from glist where id=%s"%(id)
        n, rows = db.query(sql)
        # ret.gift_info = out_farmat_glist(rows,0)

    except Exception, e:
        ret.code = 1010
        ret.msg = str(e)
    return HttpResponse(ret.toJson())

@csrf_exempt
def glist_list(request):
    ret = ReturnJ()
    ret.glist=[]
    try:
        # info = INFO(request)
        # info.checkPostEmpty("cur_page", "pagesize")
        # info.cur_page = str(int(info.cur_page) * int(info.pagesize))
        ret.glist = get_glist_list()

    except Exception, e:
        # print(str(e))
        ret.code = 1010
        ret.msg = str(e)

    return HttpResponse(ret.toJson())

@csrf_exempt
def glist_item(request):
    ret = ReturnJ()
    # db =DB.getDB()
    try:
        info = INFO(request)
        info.checkPostEmpty("cur_page", "pagesize",'id')
        info.cur_page = str(int(info.cur_page) * int(info.pagesize))
        ret.video_list = get_glist_item(info.id,info.cur_page,info.pagesize)
    except Exception, e:
        ret.code = 1010
        ret.msg = str(e)
    return HttpResponse(ret.toJson())